package main

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"io"
	"io/ioutil"
	"net/http"
	"regexp"
)

var regex_a string = `<a href="http://www.zhenai.com/zhenghun/[0-9a-z]+"[^>]*>[\p{Han}]+</a>`

var regex_a_group = `(?P<city>http://www.zhenai.com/zhenghun/[0-9a-z]+)"[^>]*>(?P<url>[\p{Han}]+)`

var regex_a_group_a = `<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)"[^>]*>([\p{Han}]+)</a>`

var a_map map[string]string = make(map[string]string)

var client *http.Client = http.DefaultClient

func main() {

	request, _ := http.NewRequest(http.MethodGet, "http://www.zhenai.com/zhenghun", nil)

	bytes, e := fetcher(request)

	if e != nil  {

		fmt.Println("fetcher err " , e)
	}

	var cityLists [][]string = findCityLists(bytes)

	for _, v := range cityLists {

		fmt.Println(v)
	}

}

func findCityLists(src [] byte) [][]string {

	compile := regexp.MustCompile(regex_a_group_a)

	cityLists := compile.FindAllStringSubmatch(string(src), -1)

	for i, v := range cityLists {

		cityLists[i] = []string{v[2], v[1]}

	}

	return cityLists
}

func fetcher(request *http.Request) ([]byte, error) {

	response, doErr := client.Do(request)

	defer response.Body.Close()

	if doErr != nil {

		return nil, doErr
	}

	if response.StatusCode == http.StatusOK {

		//返回编码encoding
		e := determineEncoding(response.Body)

		utf8Reader := transform.NewReader(response.Body, e.NewDecoder())

		return ioutil.ReadAll(utf8Reader)

	} else {

		return nil, fmt.Errorf("response.StatusCode is %d ", response.StatusCode)
	}

}

//返回html编码
func determineEncoding(r io.Reader) encoding.Encoding {

	bytes, err := bufio.NewReader(r).Peek(1024)

	if err != nil {

		return unicode.UTF8
	}

	e, name, _ := charset.DetermineEncoding(bytes, "")

	fmt.Println("charsetName", name)

	return e
}
